package top.jiaman.javaweb.dao;

import top.jiaman.javaweb.entity.Customer;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;

/**
 * @Author Jiaman
 * @Date 2020/11/22 20:29
 * @Desc
 */

public class CustomerDAOImpl extends BaseDAO implements CustomerDAO {
    @Override
    public void insert(Customer cust) {
        String sql = "insert into customers(name,email,birth)values(?,?,?)";
        update(sql, cust.getName(), cust.getEmail(), cust.getBirth());
    }

    @Override
    public void deleteById(int id) {
        String sql = "delete from customers where id = ?";
        update(sql, id);
    }

    @Override
    public void update(Customer cust) {
        String sql = "update customers set name = ?,email = ?,birth = ? where id = ?";
        update(sql, cust.getName(), cust.getEmail(), cust.getBirth(), cust.getId());
    }

    @Override
    public Customer getCustomerById(int id) {
        String sql = "select id,name,email,birth from customers where id = ?";
        Customer customer = getInstance(Customer.class, sql, id);
        return customer;
    }

    @Override
    public List<Customer> getAll() {
        String sql = "select id,name,email,birth from customers";
        List<Customer> list = getForList(Customer.class, sql);
        return list;
    }

    @Override
    public Long getCount() {
        String sql = "select count(*) from customers";
        return getValue(sql);
    }

    @Override
    public Date getMaxBirth() {
        String sql = "select max(birth) from customers";
        return getValue(sql);
    }
}
